<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>paths.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_math_paths.js.html">paths.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2010 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>
<a name="line16"></a>/**
<a name="line17"></a> * @fileoverview Factories for common path types.
<a name="line18"></a> * @author nicksantos@google.com (Nick Santos)
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.math.paths&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.math.Coordinate&#39;);
<a name="line25"></a>goog.require(&#39;goog.math.Path&#39;);
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>/**
<a name="line29"></a> * Defines a regular n-gon by specifing the center, a vertex, and the total
<a name="line30"></a> * number of vertices.
<a name="line31"></a> * @param {goog.math.Coordinate} center The center point.
<a name="line32"></a> * @param {goog.math.Coordinate} vertex The vertex, which implicitly defines
<a name="line33"></a> *     a radius as well.
<a name="line34"></a> * @param {number} n The number of vertices.
<a name="line35"></a> * @return {!goog.math.Path} The path.
<a name="line36"></a> */
<a name="line37"></a>goog.math.paths.createRegularNGon = function(center, vertex, n) {
<a name="line38"></a>  var path = new goog.math.Path();
<a name="line39"></a>  path.moveTo(vertex.x, vertex.y);
<a name="line40"></a>
<a name="line41"></a>  var startAngle = Math.atan2(vertex.y - center.y, vertex.x - center.x);
<a name="line42"></a>  var radius = goog.math.Coordinate.distance(center, vertex);
<a name="line43"></a>  for (var i = 1; i &lt; n; i++) {
<a name="line44"></a>    var angle = startAngle + 2 * Math.PI * (i / n);
<a name="line45"></a>    path.lineTo(center.x + radius * Math.cos(angle),
<a name="line46"></a>                center.y + radius * Math.sin(angle));
<a name="line47"></a>  }
<a name="line48"></a>  path.close();
<a name="line49"></a>  return path;
<a name="line50"></a>};
<a name="line51"></a>
<a name="line52"></a>
<a name="line53"></a>/**
<a name="line54"></a> * Defines an arrow.
<a name="line55"></a> * @param {goog.math.Coordinate} a Point A.
<a name="line56"></a> * @param {goog.math.Coordinate} b Point B.
<a name="line57"></a> * @param {?number} aHead The size of the arrow head at point A.
<a name="line58"></a> *     0 omits the head.
<a name="line59"></a> * @param {?number} bHead The size of the arrow head at point B.
<a name="line60"></a> *     0 omits the head.
<a name="line61"></a> * @return {!goog.math.Path} The path.
<a name="line62"></a> */
<a name="line63"></a>goog.math.paths.createArrow = function(a, b, aHead, bHead) {
<a name="line64"></a>  var path = new goog.math.Path();
<a name="line65"></a>  path.moveTo(a.x, a.y);
<a name="line66"></a>  path.lineTo(b.x, b.y);
<a name="line67"></a>
<a name="line68"></a>  var angle = Math.atan2(b.y - a.y, b.x - a.x);
<a name="line69"></a>  if (aHead) {
<a name="line70"></a>    path.appendPath(
<a name="line71"></a>        goog.math.paths.createRegularNGon(
<a name="line72"></a>            new goog.math.Coordinate(
<a name="line73"></a>                a.x + aHead * Math.cos(angle),
<a name="line74"></a>                a.y + aHead * Math.sin(angle)),
<a name="line75"></a>            a, 3));
<a name="line76"></a>  }
<a name="line77"></a>  if (bHead) {
<a name="line78"></a>    path.appendPath(
<a name="line79"></a>        goog.math.paths.createRegularNGon(
<a name="line80"></a>            new goog.math.Coordinate(
<a name="line81"></a>                b.x + bHead * Math.cos(angle + Math.PI),
<a name="line82"></a>                b.y + bHead * Math.sin(angle + Math.PI)),
<a name="line83"></a>            b, 3));
<a name="line84"></a>  }
<a name="line85"></a>  return path;
<a name="line86"></a>};
</pre>


</body>
</html>
